package org.hishenframework.core.web.menu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MenuContext {

	/** 菜单集合 */
	private static List<MenuItemBean> menuItems = new ArrayList<>();
	
	/**
	 * 添加菜单项目
	 * @param type
	 * @param label
	 * @param requestMapping
	 * @param order
	 */
	public static void addMenuItems(String type, String label, String requestMapping, int order) {
		MenuSubItemBean subItemBean = new MenuSubItemBean(label, requestMapping, order);
		boolean hasItem = false;
		for (MenuItemBean menuItemBean : menuItems) {
			if(menuItemBean.getType().equals(type)) {
				//将匹配的二级菜单装入一级菜单中
				menuItemBean.add(subItemBean);
				//重新排序
				hasItem = true;
				break;
			}
		}
		if (!hasItem) {
			//创建一级菜单
			MenuItemBean itemBean = new MenuItemBean(type, order);
			itemBean.add(subItemBean);
			menuItems.add(itemBean);
		}
		//TODO:可优化
		Collections.sort(menuItems);
	}

	public static MenuItemBean[] getMenuItems() {
		return menuItems.toArray(new MenuItemBean[menuItems.size()]);
	}
}
